package com.zhangck.bos_fore.action;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/** 
* @author 上午茶
* @version 创建时间：2018年5月31日 下午12:26:36 
* 类说明 
*/

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
	
	protected T model;
	
	@Override
	public T getModel() {
		return model;
	}
	
	public BaseAction() {
		 Type superclass = this.getClass().getGenericSuperclass();
		ParameterizedType parameterizedType = (ParameterizedType) superclass;
		Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		 try {
			model = modelClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			System.out.println("注入失败");
			e.printStackTrace();
		}
	}
	
	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	
	protected void pushPageDataValueStack(Page<T> page1) {
		Map<String, Object> map = new HashMap();
		map.put("total", page1.getTotalElements());//必须得用total接受
		map.put("rows", page1.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
}
